package com.example.idachuappone.person.item;

import java.util.HashMap;

import org.json.JSONObject;

import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.sina.weibo.SinaWeibo.ShareParams;
import cn.sharesdk.tencent.qzone.QZone;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;

import com.example.idachuappone.BaseActivity;
import com.example.idachuappone.LoadingActivity;
import com.example.idachuappone.R;
import com.example.idachuappone.index.dto.WelcomeDto;
import com.example.idachuappone.index.welcomentity.ShareContent;
import com.example.idachuappone.index.welcomentity.Welcome;
import com.example.idachuappone.utils.AppShareData;
import com.example.idachuappone.utils.CheckVersionUpdate;
import com.example.idachuappone.utils.ComUtil;
import com.example.idachuappone.utils.Constant;
import com.example.idachuappone.utils.FileUtil;
import com.example.idachuappone.utils.MainToast;
import com.example.idachuappone.utils.NetUtil;
import com.example.idachuappone.utils.PrefUtil;
import com.lidroid.xutils.bitmap.BitmapGlobalConfig.BitmapCacheManagementTask;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;

public class PersonSettingActivity extends BaseActivity implements OnClickListener {
	private Button btn_back;
	private CheckVersionUpdate checkVersionUpdate;
	private Dialog dialogLoading;
	private Dialog dialogShare;
	private Handler handler;
	private ImageView img_more_arr_clearPicCache;
	private boolean isClearing;
	private LinearLayout ll_update_yes;
	private int loginRequest;
	private RelativeLayout nowRL;
	private PlatformActionListener paListener;
	private ProgressBar pb_more_clearPicCache;
	private RelativeLayout rl_about_my;
	private RelativeLayout rl_clear_cache;
	private RelativeLayout rl_comment_pro;
	private RelativeLayout rl_help;
	private RelativeLayout rl_new_version;
	private RelativeLayout rl_often_address;
	private RelativeLayout rl_out_login;
	private RelativeLayout rl_share;
	private ShareContent shareContent;
	private TextView tv_img_cache_num;
	private TextView tv_update_no;
	private TextView tv_update_yes;
	private TextView txt_more_cacheSize;

	public PersonSettingActivity() {
		this.loginRequest = 15;
		this.isClearing = false;
		this.handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				switch (msg.what) {
				case 11 /* 11 */:
					PersonSettingActivity.this.pb_more_clearPicCache
							.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
					PersonSettingActivity.this.img_more_arr_clearPicCache.setVisibility(0);
					PersonSettingActivity.this.isClearing = false;
					PersonSettingActivity.this.txt_more_cacheSize.setText("\u6e05\u7406\u7f13\u5b58");
					MainToast.show(PersonSettingActivity.this, "\u6e05\u7406\u6210\u529f", 0);
					PersonSettingActivity.this.setFileNumData();
				case 111 /* 111 */:
					MainToast.show(PersonSettingActivity.this, "\u5206\u4eab\u5931\u8d25", 0);
				case 11111:
					MainToast.show(PersonSettingActivity.this, "\u5206\u4eab\u53d6\u6d88", 0);
				default:
				}
			}
		};
		this.paListener = new PlatformActionListener() {
			@Override
			public void onError(Platform platform, int action, Throwable t) {
				PersonSettingActivity.this.handler.sendEmptyMessage(111);
			}

			@Override
			public void onComplete(Platform arg0, int arg1, HashMap<String, Object> hashMap) {
				PersonSettingActivity.this.handler.sendEmptyMessage(1111);
			}

			@Override
			public void onCancel(Platform platform, int action) {
				PersonSettingActivity.this.handler.sendEmptyMessage(11111);
			}
		};
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_person_setting);
		ShareSDK.initSDK(this);
		this.dialogLoading = ComUtil.createCusProgressSmallBlackBgDialog(this);
		this.checkVersionUpdate = new CheckVersionUpdate();
		initView();
		setVersionData();
		setFileNumData();
	}

	private void setVersionData() {
		if (LoadingActivity.welcome.getVersion() != null) {
			this.rl_new_version.setEnabled(true);
			if (this.checkVersionUpdate.isYesUpdate(this, LoadingActivity.welcome.getVersion())) {
				this.rl_new_version.setOnClickListener(this);
				this.ll_update_yes.setVisibility(0);
				this.tv_update_no.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
				this.tv_update_yes.setText("V." + LoadingActivity.welcome.getVersion().getVersion());
				return;
			} else if (this.checkVersionUpdate.isUpdate(this, LoadingActivity.welcome.getVersion())) {
				this.rl_new_version.setOnClickListener(this);
				this.ll_update_yes.setVisibility(0);
				this.tv_update_no.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
				this.tv_update_yes.setText("V." + LoadingActivity.welcome.getVersion().getVersion());
				return;
			} else {
				this.rl_new_version.setEnabled(false);
				this.ll_update_yes.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
				this.tv_update_no.setVisibility(0);
				return;
			}
		}
		NetUtil.get(this, Constant.PACKAGES, 100, new RequestCallBack<String>() {
			@Override
			public void onSuccess(ResponseInfo<String> responseInfo) {
				try {
					JSONObject jsonObject = new JSONObject(NetUtil.unescapeUnicode(responseInfo.result));
					Welcome welcome = new Welcome();
					welcome.parseJson(jsonObject);
					LoadingActivity.welcome = welcome;
					PersonSettingActivity.this.setVersionData();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

			@Override
			public void onFailure(HttpException arg0, String arg1) {
			}
		});
	}

	private void setFileNumData() {
		try {
			long length = FileUtil.getFileSize(FileUtil.getCacheDir());
			if (length > 0) {
				this.tv_img_cache_num.setText(FileUtil.FormetFileSize(length));
			} else {
				this.tv_img_cache_num.setText("");
			}
		} catch (Exception e) {
			e.printStackTrace();
			this.tv_img_cache_num.setText("");
		}
	}

	private void initView() {
		this.btn_back = (Button) findViewById(R.id.btn_back);
		this.btn_back.setOnClickListener(this);
		this.rl_often_address = (RelativeLayout) findViewById(R.id.rl_often_address);
		this.rl_often_address.setOnClickListener(this);
		this.rl_share = (RelativeLayout) findViewById(R.id.rl_share);
		this.rl_share.setOnClickListener(this);
		this.rl_help = (RelativeLayout) findViewById(R.id.rl_help);
		this.rl_help.setOnClickListener(this);
		this.rl_about_my = (RelativeLayout) findViewById(R.id.rl_about_my);
		this.rl_about_my.setOnClickListener(this);
		this.rl_comment_pro = (RelativeLayout) findViewById(R.id.rl_comment_pro);
		this.rl_comment_pro.setOnClickListener(this);
		this.rl_new_version = (RelativeLayout) findViewById(R.id.rl_new_version);
		this.rl_new_version.setOnClickListener(this);
		this.ll_update_yes = (LinearLayout) findViewById(R.id.ll_update_yes);
		this.tv_update_no = (TextView) findViewById(R.id.tv_update_no);
		this.tv_update_yes = (TextView) findViewById(R.id.tv_update_yes);
		this.rl_clear_cache = (RelativeLayout) findViewById(R.id.rl_clear_cache);
		this.rl_clear_cache.setOnClickListener(this);
		this.tv_img_cache_num = (TextView) findViewById(R.id.tv_img_cache_num);
		this.rl_out_login = (RelativeLayout) findViewById(R.id.rl_out_login);
		this.rl_out_login.setOnClickListener(this);
		this.img_more_arr_clearPicCache = (ImageView) findViewById(R.id.img_more_arr_clearPicCache);
		this.pb_more_clearPicCache = (ProgressBar) findViewById(R.id.pb_more_clearPicCache);
		this.txt_more_cacheSize = (TextView) findViewById(R.id.txt_more_cacheSize);
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (PrefUtil.getInstance(this).getUID() == null || PrefUtil.getInstance(this).getUID().length() <= 0) {
			this.rl_out_login.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
		} else {
			this.rl_out_login.setVisibility(0);
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_back /* 2131165198 */:
			finish();
		case R.id.rl_often_address /* 2131165390 */:
			if (PrefUtil.getInstance(this).getUID() == null) {
				this.nowRL = this.rl_often_address;
				showLogin();
				return;
			}
			startActivity(new Intent(this, CookAddressListPersonActivity.class));
		case R.id.rl_share /* 2131165391 */:
			this.shareContent = LoadingActivity.welcome.getShare();
			if (this.shareContent == null) {
				NetUtil.get(this, Constant.PACKAGES, 100, new RequestCallBack<String>() {
					@Override
					public void onStart() {
						super.onStart();
						PersonSettingActivity.this.dialogLoading.show();
					}

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						PersonSettingActivity.this.dialogLoading.dismiss();
						String json = NetUtil.unescapeUnicode(responseInfo.result);
						try {
							LoadingActivity.welcome = new WelcomeDto().getWelcome(PersonSettingActivity.this, json);
							PersonSettingActivity.this.shareContent = LoadingActivity.welcome.getShare();
							PersonSettingActivity.this.dialogShare = PersonSettingActivity.this
									.createBottomShareDialog();
							PersonSettingActivity.this.dialogShare.show();
						} catch (Exception e) {
							MainToast.show(PersonSettingActivity.this, e.getMessage(), 0);
						}
					}

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						PersonSettingActivity.this.dialogLoading.dismiss();
						PersonSettingActivity.this.shareContent = new ShareContent(
								"http://www.idachu.cn/kitchener/recommend",
								"\u4e0d\u60f3\u505a\u996d\uff0c\u8bf7\u4e2a\u5927\u53a8\u5427",
								"\u547c\u53eb\u4e13\u4e1a\u7ea7\u5927\u53a8\u4e0a\u95e8\u70f9\u996a\uff0c\u56db\u83dc69\u5143\uff0c\u516d\u83dc99\u5143\uff0c\u8986\u76d6\u5317\u4eac\u4e94\u73af\u5185\u3002\u5feb\u6765\u4e0b\u8f7d\u5427~",
								"http://www.idachu.cn/icon/images/share.png?ver=2a6d26df");
						PersonSettingActivity.this.dialogShare = PersonSettingActivity.this.createBottomShareDialog();
						PersonSettingActivity.this.dialogShare.show();
					}
				});
				return;
			}
			this.dialogShare = createBottomShareDialog();
			this.dialogShare.show();
		case R.id.rl_comment_pro /* 2131165392 */:
			try {
				startActivity(new Intent("android.intent.action.VIEW",
						Uri.parse("market://details?id=com.example.idachuappone")));
			} catch (Exception e) {
				MainToast.show(this, "\u6ca1\u6709\u627e\u5230\u5e94\u7528\u5e02\u573a", 0);
			}
		case R.id.rl_new_version /* 2131165393 */:
			Intent intenturi = new Intent("android.intent.action.VIEW");
			intenturi.setData(Uri.parse(LoadingActivity.welcome.getVersion().getAndroid()));
			startActivity(intenturi);
		case R.id.rl_clear_cache /* 2131165397 */:
			if (FileUtil.SDCardExist().booleanValue() && !this.isClearing) {
				new Builder(this).setMessage("\u662f\u5426\u6e05\u9664\u7f13\u5b58\uff1f")
						.setPositiveButton("\u6e05\u9664", new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int which) {
								PersonSettingActivity.this.isClearing = true;
								PersonSettingActivity.this.pb_more_clearPicCache.setVisibility(0);
								PersonSettingActivity.this.img_more_arr_clearPicCache
										.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
								PersonSettingActivity.this.txt_more_cacheSize.setText("\u6b63\u5728\u6e05\u7406...");
								PersonSettingActivity.this.clearCache();
							}
						}).setNegativeButton("\u53d6\u6d88", new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int which) {
							}
						}).show();
			}
		case R.id.rl_help /* 2131165402 */:
			startActivity(new Intent(this, HelpActivity.class));
		case R.id.rl_about_my /* 2131165403 */:
			startActivity(new Intent(this, AboutActivity.class));
		case R.id.rl_out_login /* 2131165404 */:
			new Builder(this).setMessage("\u786e\u5b9a\u9000\u51fa\u6b64\u5e10\u53f7")
					.setPositiveButton("\u786e\u5b9a", new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							PrefUtil.getInstance(PersonSettingActivity.this).setUID(null);
							PrefUtil.getInstance(PersonSettingActivity.this).setAuth(null);
							PrefUtil.getInstance(PersonSettingActivity.this).setMa(null);
							PrefUtil.getInstance(PersonSettingActivity.this).setFimallyLocateCoor(null);
							PrefUtil.getInstance(PersonSettingActivity.this).setArea(null);
							PrefUtil.getInstance(PersonSettingActivity.this).setXiaoQu(null);
							PrefUtil.getInstance(PersonSettingActivity.this).setDoor(null);
							PrefUtil.getInstance(PersonSettingActivity.this).setName(null);
							PrefUtil.getInstance(PersonSettingActivity.this).setUserAddressId(null);
							PrefUtil.getInstance(PersonSettingActivity.this).setPhone(null);
							PrefUtil.getInstance(PersonSettingActivity.this).setYouhuiPersonDianId("0");
							AppShareData.isPersonFrag = true;
							AppShareData.isClearData = true;
							PersonSettingActivity.this.rl_out_login
									.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
							PersonSettingActivity.this.finish();
						}
					}).setNegativeButton("\u53d6\u6d88", new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
						}
					}).create().show();
		case R.id.ll_dismiss /* 2131165441 */:
		case R.id.txt_share_sel_cancel /* 2131165630 */:
			this.dialogShare.dismiss();
		case R.id.lLayout_share_to_weixin /* 2131165622 */:
			this.dialogShare.dismiss();
			ShareParams spweixin = new ShareParams();
			spweixin.setTitle(this.shareContent.getTitle());
			spweixin.setText(this.shareContent.getContent());
			spweixin.setImageUrl(this.shareContent.getImg());
			spweixin.setUrl(this.shareContent.getUrl());
			spweixin.setShareType(4);
			Platform weixin = ShareSDK.getPlatform(Wechat.NAME);
			weixin.setPlatformActionListener(this.paListener);
			weixin.share(spweixin);
		case R.id.lLayout_share_to_wxpy /* 2131165624 */:
			this.dialogShare.dismiss();
			ShareParams spweixinf = new ShareParams();
			spweixinf.setTitle(this.shareContent.getContent());
			spweixinf.setImageUrl(this.shareContent.getImg());
			spweixinf.setUrl(this.shareContent.getUrl());
			spweixinf.setShareType(4);
			Platform weixinf = ShareSDK.getPlatform(WechatMoments.NAME);
			weixinf.setPlatformActionListener(this.paListener);
			weixinf.share(spweixinf);
		case R.id.lLayout_share_to_qq /* 2131165626 */:
			this.dialogShare.dismiss();
			ShareParams sp1 = new ShareParams();
			sp1.setTitle(this.shareContent.getTitle());
			sp1.setTitleUrl(this.shareContent.getUrl());
			sp1.setText(this.shareContent.getContent());
			sp1.setImageUrl(this.shareContent.getImg());
			sp1.setSite(getString(2131230803));
			sp1.setSiteUrl(this.shareContent.getUrl());
			Platform qzone = ShareSDK.getPlatform(QZone.NAME);
			qzone.setPlatformActionListener(this.paListener);
			qzone.share(sp1);
		case R.id.lLayout_share_to_sina /* 2131165628 */:
			this.dialogShare.dismiss();
			ShareParams sp = new ShareParams();
			sp.setText(this.shareContent.getContent());
			sp.setImageUrl(this.shareContent.getImg());
			Platform weibo = ShareSDK.getPlatform(SinaWeibo.NAME);
			weibo.setPlatformActionListener(this.paListener);
			weibo.share(sp);
		default:
		}
	}

	public void showLogin() {
		MainToast.show(this, "\u8bf7\u5148\u767b\u5f55", 0);
		startActivityForResult(new Intent(this, LoginActivity.class), this.loginRequest);
	}

	private void clearCache() {
		new Thread(new Runnable() {
			@Override
			public void run() {
				FileUtil.delAllFile(FileUtil.getCacheDir());
				PersonSettingActivity.this.handler.sendEmptyMessage(11);
			}
		}).start();
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == this.loginRequest && resultCode == -1 && this.nowRL != null) {
			this.nowRL.performClick();
		}
	}

	private Dialog createBottomShareDialog() {
		View view = LayoutInflater.from(this).inflate(R.layout.view_bottom_dialog_share, null);
		view.setMinimumWidth(getWindowManager().getDefaultDisplay().getWidth());
		view.setMinimumHeight(getWindowManager().getDefaultDisplay().getHeight());
		((LinearLayout) view.findViewById(R.id.ll_anim)).startAnimation(AnimationUtils.loadAnimation(this,
				R.anim.dialog_share_content_in));
		((LinearLayout) view.findViewById(R.id.ll_dismiss)).setOnClickListener(this);
		((LinearLayout) view.findViewById(R.id.lLayout_share_to_qq)).setOnClickListener(this);
		((LinearLayout) view.findViewById(R.id.lLayout_share_to_sina)).setOnClickListener(this);
		((LinearLayout) view.findViewById(R.id.lLayout_share_to_weixin)).setOnClickListener(this);
		((LinearLayout) view.findViewById(R.id.lLayout_share_to_wxpy)).setOnClickListener(this);
		((TextView) view.findViewById(R.id.txt_share_sel_cancel)).setOnClickListener(this);
		Dialog dialog = new Dialog(this, 2131361803);
		dialog.setContentView(view);
		dialog.setCanceledOnTouchOutside(false);
		Window dialogWindow = dialog.getWindow();
		LayoutParams lp = dialogWindow.getAttributes();
		dialogWindow.setGravity(83);
		lp.x = 0;
		lp.y = 0;
		dialogWindow.setAttributes(lp);
		return dialog;
	}
}
